window.onload = function () {

    $(function () {

        //阅读勾选
        $('.form .yuedubox').click(function () {
            if ($(this).prop('checked')) {
                $('button').css({
                    background: '#B52024'
                })
            } else {
                $('button').css({
                    background: '#9a9a9a'
                })
            }



        })

        //表单条件验证

        $.validator.addMethod("checkName", function (val) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,18}$/.test(val);
        })

        $.validator.addMethod("checkPwd", function (val) {
            return /^[a-zA-Z0-9_]{6,18}$/.test(val);
        })

        $.validator.addMethod("checkPhone", function (val) {
            return /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(val);
        })



        $('form').validate({
            rules: {
                uname: {
                    checkName: true,
                    required: true,

                },
                upwd: {
                    checkPwd: true,
                    required: true,

                },
                upwds: {
                    equalTo: "#pwd"
                },
                uphone: {
                    checkPhone: true,
                    digits: true,
                    required: true,

                }
            },
            messages: {
                uname: {
                    required: '用户名必填',
                    checkName: '以字母开头,长度为6-18位字符'
                },
                upwd: {
                    required: '密码必填',
                    checkPwd: '以字母开头，长度在6~18之间，只能包含字母、数字和下划线'
                },
                upwds: {
                    equalTo: "两次输入的密码不一致"
                },
                uphone: {
                    digits: '只能是数字',
                    required: '手机号必须要填写',
                    checkPhone: '请输入正确的手机号'
                }
            },
            submitHandler() {

                console.log(666);
                $.ajax({
                    url: './account/reg',
                    type: 'post',
                    data: $('form').serialize() //前端发送到服务器的数据
                }).then(function (response) {
                    console.log(response);

                    if (response.status == 200) {
                        layer.msg('注册成功')

                        setTimeout(function () {
                            location = "login.html";
                        }, 1000)
                    } else {
                        layer.msg('注册失败')
                    }


                })
            }




        })











    })

}